package com.lianxian.xindong.life.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lianxian.xindong.life.R;
import com.lianxian.xindong.life.entities.Food;
import com.lianxian.xindong.life.util.GlideUtil;

import java.util.List;


public class FoodAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;
    private Context context;
    List<Food> list;

    public FoodAdapter(Context context, List<Food> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Food food= list.get(position);
        ViewHolder viewHolder;
        if (convertView==null){
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_food_layout,parent,false);
            viewHolder.foodIV = convertView.findViewById(R.id.food_pic);
            viewHolder.authorNameTV = convertView.findViewById(R.id.author_name);
            viewHolder.addressTV= convertView.findViewById(R.id.food_address);
            viewHolder.descTV = convertView.findViewById(R.id.food_desc);
            viewHolder.distanceTV = convertView.findViewById(R.id.food_distance);
            viewHolder.likeCount = convertView.findViewById(R.id.food_like_count);
            viewHolder.likeIV = convertView.findViewById(R.id.food_like_pic);
            viewHolder.authorHeadIV = convertView.findViewById(R.id.food_author_headpic);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        GlideUtil.GlideWithPlaceHolder(context, food.getPicUrl()).into(viewHolder.foodIV);
        viewHolder.authorNameTV.setText(food.getAuthor());
        viewHolder.addressTV.setText(food.getAddress());
        viewHolder.descTV.setText(food.getDesc());
        viewHolder.distanceTV.setText(food.getDistance()+"km");
        viewHolder.likeCount.setText(food.getLikeCount()+"");
        if (food.isLikeOrNot()){
            viewHolder.likeIV.setImageResource(R.mipmap.xst_frame_1);
        }else {
            viewHolder.likeIV.setImageResource(R.mipmap.xst_frame);
        }
        GlideUtil.GlideWithPlaceHolder(context, food.getAuthorHeadPicUrl()).into(viewHolder.authorHeadIV);
        return convertView;
    }

    private class ViewHolder{
        private ImageView foodIV;
        private TextView authorNameTV;
        private ImageView authorHeadIV;
        private TextView descTV;
        private ImageView likeIV;
        private TextView likeCount;
        private TextView addressTV;
        private TextView distanceTV;

        public ViewHolder() {

        }

    }
}
